from django.db import models
from users.models import UserModel
# Create your models here.


class SortsModel(models.Model):
    name = models.CharField(max_length=200)
    pid = models.ForeignKey('self', on_delete=models.SET_NULL,db_constraint=False,null=True
                            ,blank=True,related_name='sorts_pid',verbose_name='父级分类')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '分类表'
        verbose_name_plural = verbose_name


class GoodsModel(models.Model):
    name = models.CharField(max_length=200,verbose_name="商品名称")
    pid = models.ForeignKey(SortsModel, on_delete=models.SET_NULL, null=True, blank=True,db_constraint=False,
                            related_name='goods_pid', verbose_name='分类')
    topid = models.ForeignKey(SortsModel, on_delete=models.SET_NULL, null=True, blank=True,db_constraint=False,
                            related_name='goods_topid', verbose_name='顶级分类')
    sales = models.IntegerField(verbose_name="销量")
    store = models.IntegerField(verbose_name="库存",default=10)
    lockstore = models.IntegerField(verbose_name="锁定库存", default=0)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="价格",default=230)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '商品表'
        verbose_name_plural = verbose_name


class BrowseModel(models.Model):
    user = models.ForeignKey(UserModel,on_delete=models.CASCADE,db_constraint=False,
                             verbose_name='用户',related_name='browse_user')
    goods = models.ForeignKey(GoodsModel,on_delete=models.CASCADE,db_constraint=False,verbose_name='商品',
                              related_name='browse_goods')
    topid = models.ForeignKey(SortsModel, on_delete=models.SET_NULL, null=True, blank=True,db_constraint=False,
                              related_name='browse_topid', verbose_name='顶级分类')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    count = models.IntegerField(verbose_name='浏览次数')
    def __str__(self):
        return self.user.username

    class Meta:
        verbose_name = '浏览记录表'
        verbose_name_plural = verbose_name






